{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 预测锅炉燃烧效率\n",
    "\n",
    "## 问题背景\n",
    "\n",
    "<P>\n",
    "火力发电的基本原理是利用燃料在燃烧时加热水生成蒸汽，然后利用蒸汽压力推动汽轮机旋转，再通过汽轮机带动发电机旋转，最终产生电能。在这一系列的能量转化中，锅炉的燃烧效率起着关键作用，因为它影响着燃料燃烧后加热水产生高温高压蒸汽的能力。\n",
    "锅炉的燃烧效率受到多种因素的影响。首先是锅炉的可调参数，例如燃烧给量、一二次风、引风、返料风和给水水量等。这些参数的调整会影响燃料的燃烧情况，进而影响蒸汽的产生量和质量。其次，锅炉的工况也是影响燃烧效率的重要因素，包括锅炉床温、床压，炉膛温度、压力以及过热器的温度等。这些工况参数会直接影响燃烧的稳定性和效率。\n",
    "为了预测产生的蒸汽量，需要经脱敏后的锅炉传感器采集的数据。这些数据包含了锅炉的各项参数，采集频率是分钟级别。可以利用训练数据(train.txt)来训练出模型，然后应用该模型对测试数据(test.txt)中的目标变量进行预测。预测结果的MSE(mean square error)或者决定系数R^2将被用来评估模型的表现\n",
    "</P>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2888, 39)\n",
      "(2166, 38)\n",
      "(722, 38)\n"
     ]
    }
   ],
   "source": [
    "# 采集训练集数据\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "filename_train = r'F:\\机器学习数据集\\burn_train.txt'\n",
    "\n",
    "with open(filename_train,encoding='utf-8') as f1:\n",
    "    data = np.loadtxt(f1,skiprows=1,delimiter='\\t')\n",
    "\n",
    "print(data.shape)\n",
    "# 划分特征矩阵与标签列表\n",
    "X_train,X_test,y_train,y_test = train_test_split(data[:,:-1],data[:,-1])\n",
    "print(X_train.shape)\n",
    "print(X_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-02T02:08:54.781680800Z",
     "start_time": "2023-10-02T02:08:47.874843400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集的决定系数: 0.831\n",
      "测试集的均方误差: 0.153\n"
     ]
    }
   ],
   "source": [
    "# 尝试使用神经网络做训练\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.neural_network import MLPRegressor\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "scaler = StandardScaler()\n",
    "mlp_regressor = MLPRegressor(max_iter=1000)\n",
    "pipe = Pipeline([('scaler',scaler),('mlp',mlp_regressor)])\n",
    "pipe.fit(X_train,y_train)\n",
    "print('测试集的决定系数:',round(pipe.score(X_test,y_test),3))\n",
    "y_pred = pipe.predict(X_test)\n",
    "mse = mean_squared_error(y_test,y_pred)\n",
    "print('测试集的均方误差:',round(mse,3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-29T11:57:33.423443400Z",
     "start_time": "2023-09-29T11:57:26.423295300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数集合: {'mlp__activation': 'logistic', 'mlp__hidden_layer_sizes': (100,), 'mlp__learning_rate_init': 0.001}\n",
      "验证集决定系数 0.8919342159456838\n",
      "测试集决定系数 0.8727711485292523\n"
     ]
    }
   ],
   "source": [
    "# 训练神经网络\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.neural_network import MLPRegressor\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 设置管道\n",
    "pipe_mlp = Pipeline([('scaler',StandardScaler()),('mlp',MLPRegressor(max_iter=1000))])\n",
    "\n",
    "# 设置参数\n",
    "params_mlp = {'mlp__activation':['relu','tanh','logistic'],\n",
    "              'mlp__hidden_layer_sizes':[(100,),(100,100),(38,100,50)],\n",
    "              'mlp__learning_rate_init':[0.0001,0.001,0.01,0.1]\n",
    "              }\n",
    "\n",
    "grid_mlp = GridSearchCV(pipe_mlp,param_grid=params_mlp,n_jobs=-1)\n",
    "grid_mlp.fit(X_train,y_train)\n",
    "print(\"最佳参数集合:\",grid_mlp.best_params_)\n",
    "print(\"验证集决定系数\",grid_mlp.best_score_)\n",
    "print('测试集决定系数',grid_mlp.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-30T12:30:30.484063900Z",
     "start_time": "2023-09-30T12:27:36.289100100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集的决定系数: -0.0\n",
      "测试集的均方误差: 0.906\n"
     ]
    }
   ],
   "source": [
    "# 使用Lasso回归\n",
    "from sklearn.linear_model import Lasso\n",
    "pipe_lasso = Pipeline([('scaler',StandardScaler()),('lasso',Lasso())])\n",
    "pipe_lasso.fit(X_train,y_train)\n",
    "print('测试集的决定系数:',round(pipe_lasso.score(X_test,y_test),3))\n",
    "y_pred = pipe_lasso.predict(X_test)\n",
    "mse = mean_squared_error(y_test,y_pred)\n",
    "print('测试集的均方误差:',round(mse,3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-29T11:56:55.117013100Z",
     "start_time": "2023-09-29T11:56:52.976306500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.817\n",
      "测试集的均方误差: 0.147\n"
     ]
    }
   ],
   "source": [
    "# 随机森林\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.metrics import mean_squared_error\n",
    "rf = RandomForestRegressor()\n",
    "pipe_rf = Pipeline([('scaler',StandardScaler()),('rf',rf)])\n",
    "pipe_rf.fit(X_train,y_train)\n",
    "y_pred=pipe_rf.predict(X_test)\n",
    "print(\"测试集精度:\",round(rf.score(X_test,y_test),3))\n",
    "mse = mean_squared_error(y_test,y_pred)\n",
    "print('测试集的均方误差:',round(mse,3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-01T03:27:21.748047400Z",
     "start_time": "2023-10-01T03:27:17.819064500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'rf__max_depth': 9, 'rf__n_estimators': 200}\n",
      "验证集最好的决定系数: 0.8514819871404423\n",
      "测试集的决定系数: 0.8208062584278109\n"
     ]
    }
   ],
   "source": [
    "# 训练随机森林(PCA降维)\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理\n",
    "scaler = StandardScaler()\n",
    "# pca降维\n",
    "pca = PCA(n_components=28)\n",
    "# 随机森林模型\n",
    "rf = RandomForestRegressor()\n",
    "# 参数\n",
    "params_rf = {#'pca__n_components':[15,25,30],\n",
    "             #'rf__criterion':['gini', 'entropy'],\n",
    "             'rf__n_estimators':[100,150,200],\n",
    "             'rf__max_depth':[7,8,9,10]\n",
    "             }\n",
    "# 搭建管道\n",
    "pipe_rf = Pipeline([('scaler',scaler),('pca',pca),('rf',rf)])\n",
    "# 带交叉验证的网格搜索\n",
    "grid_rf = GridSearchCV(pipe_rf,param_grid=params_rf,n_jobs=-1)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_rf.best_params_)\n",
    "print(\"验证集最好的决定系数:\",grid_rf.best_score_)\n",
    "print(\"测试集的决定系数:\",grid_rf.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-01T03:35:16.053579100Z",
     "start_time": "2023-10-01T03:34:37.557031600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'rf__max_depth': 10, 'rf__n_estimators': 250}\n",
      "验证集最好的决定系数: 0.8749055084186754\n",
      "测试集的决定系数: 0.8402803193287496\n"
     ]
    }
   ],
   "source": [
    "# 训练随机森林\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理\n",
    "scaler = StandardScaler()\n",
    "\n",
    "# 随机森林模型\n",
    "rf = RandomForestRegressor()\n",
    "# 参数\n",
    "params_rf = {\n",
    "             #'rf__criterion':['gini', 'entropy'],\n",
    "             'rf__n_estimators':[150,200,250],\n",
    "             'rf__max_depth':[7,8,9,10]\n",
    "             }\n",
    "# 搭建管道\n",
    "pipe_rf = Pipeline([('scaler',scaler),('rf',rf)])\n",
    "# 带交叉验证的网格搜索\n",
    "grid_rf = GridSearchCV(pipe_rf,param_grid=params_rf,n_jobs=-1)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_rf.best_params_)\n",
    "print(\"验证集最好的决定系数:\",grid_rf.best_score_ )\n",
    "print(\"测试集的决定系数:\",grid_rf.score(X_test,y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-01T03:32:14.011087600Z",
     "start_time": "2023-10-01T03:31:12.970110700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度: 0.836\n",
      "测试集的均方误差: 0.148\n"
     ]
    }
   ],
   "source": [
    "# 支持向量回归\n",
    "from sklearn.svm import SVR\n",
    "svr = SVR()\n",
    "pipe_scr = Pipeline([('scaler',StandardScaler()),('svr',svr)])\n",
    "pipe_scr.fit(X_train,y_train)\n",
    "y_pred=pipe_scr.predict(X_test)\n",
    "print(\"测试集精度:\",round(pipe_scr.score(X_test,y_test),3))\n",
    "mse = mean_squared_error(y_test,y_pred)\n",
    "print('测试集的均方误差:',round(mse,3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-29T12:13:19.740529Z",
     "start_time": "2023-09-29T12:13:19.124529600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'pca__n_components': 30, 'svr__C': 1, 'svr__gamma': 0.01, 'svr__kernel': 'rbf'}\n",
      "最佳验证集决定系数: 0.877\n",
      "最佳测试集决定系数: 0.877\n"
     ]
    }
   ],
   "source": [
    "# 调整svr的参数\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.decomposition import PCA\n",
    "param_svr = {'pca__n_components':[20,25,30],\n",
    "             'svr__kernel':['rbf','linear','sigmoid'],\n",
    "             'svr__gamma':[0.001,0.01,0.1,1,10],\n",
    "             'svr__C':[0.001,0.01,0.1,1]}\n",
    "\n",
    "pipe_svr = Pipeline([('scaler',StandardScaler()),('pca',PCA()),('svr',SVR())])\n",
    "grid_svr = GridSearchCV(pipe_svr,param_grid=param_svr,n_jobs=-1)\n",
    "grid_svr.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_svr.best_params_)\n",
    "print(\"最佳验证集决定系数:\",round(grid_svr.best_score_,3))\n",
    "print(\"最佳测试集决定系数:\",round(grid_svr.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-02T02:27:54.235725700Z",
     "start_time": "2023-10-02T02:26:56.932727800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数组合: {'svr__C': 0.1, 'svr__gamma': 0.001, 'svr__kernel': 'linear'}\n",
      "最佳验证集决定系数: 0.883\n",
      "最佳测试集决定系数: 0.882\n"
     ]
    }
   ],
   "source": [
    "# 不做降维处理\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.decomposition import PCA\n",
    "param_svr = {\n",
    "             'svr__kernel':['rbf','linear','sigmoid'],\n",
    "             'svr__gamma':[0.001,0.01,0.1,1,10],\n",
    "             'svr__C':[0.001,0.01,0.1,1]}\n",
    "\n",
    "pipe_svr = Pipeline([('scaler',StandardScaler()),('svr',SVR())])\n",
    "grid_svr = GridSearchCV(pipe_svr,param_grid=param_svr,n_jobs=-1)\n",
    "grid_svr.fit(X_train,y_train)\n",
    "print(\"最佳参数组合:\",grid_svr.best_params_)\n",
    "print(\"最佳验证集决定系数:\",round(grid_svr.best_score_,3))\n",
    "print(\"最佳测试集决定系数:\",round(grid_svr.score(X_test,y_test),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-02T02:31:12.852613700Z",
     "start_time": "2023-10-02T02:30:49.655596900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
